package com.example.mycontacts;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddNewContact extends Activity implements TextWatcher{

	private Spinner spinner_categories;
  	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_new_contact);
		EditText text;
		text = (EditText) findViewById(R.id.name);
		text.addTextChangedListener(this);
	
		addItemsOnSpinnerCats();
    	addListenerOnSpinnerItemSelection();
		
		final Button button_add = (Button) findViewById(R.id.button_add);
        button_add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	saveInformation();
            }
        });
       
        final Button button_cancel = (Button) findViewById(R.id.button_cancel);
        button_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	//refreshes the mainScreen
            	Intent intent = new Intent(AddNewContact.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    
                startActivity(intent);
            	
            }
        });
	}
	
	public void addItemsOnSpinnerCats() {
		
		spinner_categories = (Spinner) findViewById(R.id.category_spinner);
		List<String> list = new ArrayList<String>();
		DBAdapter db = new DBAdapter(this);
		db.open();
		
		//populates the spinner from the list acquired from database
		list = db.getCats();

		db.close();
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner_categories.setAdapter(dataAdapter);
	}
	
	public void addListenerOnSpinnerItemSelection() {
		spinner_categories = (Spinner) findViewById(R.id.category_spinner);
		spinner_categories.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	  }

	public void addListenerOnButton() {
		 
		spinner_categories = (Spinner) findViewById(R.id.category_spinner);
	 
	  }
	
	public void gohomenow(View view){
		Intent startMain = new Intent(Intent.ACTION_MAIN);
		startMain.addCategory(Intent.CATEGORY_LAUNCHER);
		startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(startMain);
	}
		//Use that to search database
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_add_new_contact, menu);
		return true;
	}
		@Override
		public void onTextChanged(CharSequence s, int start, int count, int after)
		{
			//enableButton();
		}
				
		@Override
		public void beforeTextChanged(CharSequence s, int start, int before, int end)
		{
			//enableButton();
		}
				
		@Override
		public void afterTextChanged(Editable s)
		{
			//enableButton();
		}
		
		
		
		public void initSaveButton(){
			//Information info = new Information();
			Button btnSave = (Button) findViewById(R.id.button_add);
			btnSave.setOnClickListener(new View.OnClickListener() {
				
				//tells the button what to do when clicked
				public void onClick(View v) {
					saveInformation();
					//saveAddress();
					
				}
			});
			//this will take the name in the EditText field corresponded to the name
			//and see if it is null,  when the name edittext field is not null button will
			//be enabled
			
		}
		
		public void enableButton(){
			//this will take the name in the EditText field corresponded to the name
			//and see if it is null,  when the name edittext field is not null button will
			//be enabled
			EditText text;
			Information info = new Information();
			Button btnSave = (Button) findViewById(R.id.button_add);
			text = (EditText) findViewById(R.id.name);
			info.setName(text.getText().toString());
			if(info.getName() != null)
			{
				btnSave.setEnabled(true);
			}
			else
				btnSave.setEnabled(false);
		}

		
		public void saveInformation()
		{
			EditText text;
			Information info = new Information();
			String condition;
			
			text = (EditText) findViewById(R.id.name);
			condition = (text.getText().toString());
		
			if(condition.matches(""))
			{
				Toast.makeText(this, "Name cannot be empty", Toast.LENGTH_SHORT).show();
			}
			else
			{
				info.setName(text.getText().toString());
				text = (EditText) findViewById(R.id.phoneNumber);
				info.setPhoneNumber(text.getText().toString());
				text = (EditText) findViewById(R.id.altPhoneNumber);
				info.setAltPhone(text.getText().toString());
				text = (EditText) findViewById(R.id.email);
				info.setEmailAddress(text.getText().toString());
				text = (EditText) findViewById(R.id.comments);
				info.setComments(text.getText().toString());
				String str = spinner_categories.getItemAtPosition(spinner_categories.getSelectedItemPosition()).toString();
				info.setCategory(str);
				
				Address addr = new Address();
				text = (EditText) findViewById(R.id.address);
				addr.setStreetAddress(text.getText().toString());
				text = (EditText) findViewById(R.id.city);
				addr.setCity(text.getText().toString());
				text = (EditText) findViewById(R.id.state);
				addr.setState(text.getText().toString());
				text = (EditText) findViewById(R.id.zipcode);
				addr.setZipcode(text.getText().toString());
				
				DBAdapter db = new DBAdapter(this);
				db.open();
				long success = db.insertInformation(info, addr);
				db.close();
			
				if(success > 0)
				{
					clearDataInfo();
					clearDataAddr();
				}
			}
		}
		
		//after a successful save the data will be deleted/removed
		public void clearDataInfo()
		{
			EditText text = (EditText) findViewById(R.id.name);
			text.setText("");
			text = (EditText) findViewById(R.id.phoneNumber);
			text.setText("");
			text = (EditText) findViewById(R.id.altPhoneNumber);
			text.setText("");
			text = (EditText) findViewById(R.id.email);
			text.setText("");
			text = (EditText) findViewById(R.id.comments);
			text.setText("");
			spinner_categories.setSelection(0);			
		}
		
		//clears the text in the address fields
		public void clearDataAddr()
		{
			EditText text = (EditText) findViewById(R.id.address);
			text.setText("");
			text = (EditText) findViewById(R.id.city);
			text.setText("");
			text = (EditText) findViewById(R.id.state);
			text.setText("");
			text = (EditText) findViewById(R.id.zipcode);
			text.setText("");
		}
	
	
}
